package com.sbtr.controller;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.sbtr.business.trainingmodel.dto.ProjectEstimateListDto;
import com.sbtr.common.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.sbtr.exception.BDException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import com.sbtr.common.PageDto;
import com.sbtr.business.trainingmodel.entity.ProjectEstimate;
import com.sbtr.business.trainingmodel.dto.ProjectEstimateDto;
import com.sbtr.business.trainingmodel.service.ProjectEstimateService;
import org.springframework.web.bind.annotation.RestController;
import com.sbtr.controller.common.CommonController;

import java.util.ArrayList;
import java.util.List;

/**
 * 项目预估设置 前端控制器
 *
 * @author dgl
 * @since 2024-01-31
 */
@RestController
@RequestMapping("/projectEstimate")
@Api(tags = "项目预估设置")
public class ProjectEstimateController extends CommonController<ProjectEstimate, ProjectEstimateService> {

    @GetMapping("/pageList")
    @ApiOperation(httpMethod = "GET", value = "分页列表")
    public Response<PageDto<ProjectEstimate>> pageList(ProjectEstimateDto projectEstimateDto) {
        return Response.success(u.pageList(projectEstimateDto));
    }

    @PostMapping("/save")
    @ApiOperation(httpMethod = "POST", value = "保存")
    public Response<ProjectEstimate> save(@RequestBody ProjectEstimate projectEstimate) {
        projectEstimate.setId(null);
        if (projectEstimate.insert()) {
            return Response.success(projectEstimate);
        } else throw new BDException("保存失败");
    }

    @PostMapping("/saveSettingList")
    @ApiOperation(httpMethod = "POST", value = "保存设置列表信息")
    public Response<ProjectEstimate> saveSettingList(@RequestBody List<ProjectEstimateListDto> list, Long projectId) {
        ProjectEstimate projectEstimate = new ProjectEstimate();
        projectEstimate.setProjectId(projectId);
        JSONArray array = new JSONArray();
        for (ProjectEstimateListDto projectEstimateListDto :list) {
            JSONObject object = new JSONObject();
            object.set(projectEstimateListDto.getValue(),projectEstimateListDto.getData());
            array.add(object);
        }
        projectEstimate.setConfigSet(JSONUtil.toJsonStr(array));
        if (projectEstimate.insert()) {
            return Response.success(projectEstimate);
        } else throw new BDException("保存失败");
    }


}

